package DoExercise.HSP_ZCY.A009_图结构.A001_并查集;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: Du
 * @Date: 2021/7/20 17:18
 * <p>
 * 某个网站有N个用户，每个用户都有id_a，id_b,id_c三个信息，假设三个信息中有一条信息相同的用户都算作是一个用户，求该网站共有多少个用户
 */
public class Code01_2合并相同的用户
{
    
    
    public static void main(String[] args)
    {
        User u1 = new User("1", "2", "3");
        User u2 = new User("11", "2", "13");
        User u3 = new User("21", "21", "113");
        List<User> users = new ArrayList<>();
        users.add(u1);
        users.add(u2);
        users.add(u3);
        System.out.println(mergeUsers(users));
    }
    
    public static class User
    {
        public String id_a;
        public String id_b;
        public String id_c;
        
        public User(String a, String b, String c)
        {
            this.id_a = a;
            this.id_b = b;
            this.id_c = c;
        }
    }
    
    public static int mergeUsers(List<User> users)
    {
        Code01_1并查集.UnionSet<User> unionSet = new Code01_1并查集.UnionSet<>(users);
        HashMap<String, User> mapA = new HashMap<>();
        HashMap<String, User> mapB = new HashMap<>();
        HashMap<String, User> mapC = new HashMap<>();
        
        for (User user : users)
        {
            if (mapA.containsKey(user.id_a))
            {
                unionSet.union(user, mapA.get(user.id_a));
            } else
            {
                mapA.put(user.id_a, user);
            }
            
            if (mapB.containsKey(user.id_b))
            {
                unionSet.union(user, mapB.get(user.id_b));
            } else
            {
                mapB.put(user.id_b, user);
            }
            
            if (mapC.containsKey(user.id_c))
            {
                unionSet.union(user, mapC.get(user.id_c));
            } else
            {
                mapC.put(user.id_c, user);
            }
        }
        return unionSet.sizeMap.size();
    }
    
}
